from settings import settings
from common.base.base_handler import BaseHandler
from services.server_integration.api import ServerIntegrationApi
from services.new_services.new_server_integration.api import NewServerIntegrationApi

from services.new_services.schemas.classes import BlockType
from services.new_services.schemas.cviya_config import BLOCKS
from services.new_services.technic_page.model import TechnicPage
from services.new_services.technic_page.schemas.block_type import BlockTypeRequest
from services.new_services.new_server_integration.schemas.response_message import ResponseMessage
from services.new_services.technic_page.schemas.technic_configuration import TechnicConfiguration
from services.new_services.technic_page.schemas.current_mac_ip_configuration import TechnicPageInformationResponse


class TechnicPageHandler(BaseHandler):
    SI_API = ServerIntegrationApi()

    async def handle_set_unit_config_by_technic(self, params: dict) -> bool | ResponseMessage:
        return await self.model.set_unit_config_by_technic(TechnicConfiguration(**params['config']), params['user_role'])

    async def handle_get_cviya_list(self, params: dict) -> list[str]:
        block_type: BlockTypeRequest = BlockTypeRequest(**params['block_type'])

        if block_type.block_type in [BlockType.FSR, 'fsr']:
            return [block.cviya for block in BLOCKS if block.block_type == BlockType.FSR]
        else:
            return [block.cviya for block in BLOCKS if block.block_type == BlockType.FSC]

    async def handle_get_technic_page_information(self) -> TechnicPageInformationResponse:
        old_config = await self.SI_API.get_full_configuration()

        current_block_type = old_config.unit_configuration.unit_type
        current_block_cviya = old_config.unit_configuration.cviya
        current_serial_number = old_config.unit_configuration.main_module.serial_number
        current_mac = old_config.unit_configuration.main_module.mac_address

        current_ac_link_tx = old_config.unit_configuration.ac_link_tx
        current_ac_service_tx = old_config.unit_configuration.ac_service_tx

        current_ac_link_rx = old_config.unit_configuration.ac_link_rx
        current_acc_base_power_calibrate = old_config.unit_configuration.acc_base_power_calibrate
        current_acc_link_calibrate = old_config.unit_configuration.acc_link_calibrate
        current_acc_tx_service_calibrate = old_config.unit_configuration.acc_tx_service_calibrate

        return TechnicPageInformationResponse(block_type=current_block_type, block_cviya=current_block_cviya,
                                              mac_address=current_mac, serial_number=current_serial_number,
                                              ac_link_tx=current_ac_link_tx, ac_link_rx=current_ac_link_rx,
                                              ac_service_tx=current_ac_service_tx,
                                              acc_base_power_calibrate=current_acc_base_power_calibrate,
                                              acc_link_calibrate=current_acc_link_calibrate,
                                              acc_tx_service_calibrate=current_acc_tx_service_calibrate)

    async def handle_enable_cp(self, params: dict) -> bool:
        user_role: str = params['user_role']

        if user_role == 'adjuster':
            current_state: bool = await self.SI_API.get_production_state()
            await self.SI_API.set_production_state(not current_state)
            return True
        else:
            return False

    async def handle_get_cp_status(self):
        return await self.SI_API.get_production_state()

    async def handle_clear_alarms(self):
        await self.SI_API.clear_alarms()
        return True

    def __init__(self) -> None:
        self.model = TechnicPage()

        super(TechnicPageHandler, self).__init__(
            request_queue_uuid=settings.TECHNIC_PAGE_ROUTER_REQUEST_QUEUE
        )

        self.handle('clear_alarms', self.handle_clear_alarms)
        self.handle('get_technic_page_information', self.handle_get_technic_page_information)
        self.handle('get_cviya_list', self.handle_get_cviya_list)
        self.handle('set_unit_config_by_technic', self.handle_set_unit_config_by_technic)
        self.handle('enable_cp', self.handle_enable_cp)
        self.handle('get_cp_status', self.handle_get_cp_status)
